package com.int_tech.timeTask194;

import com.int_tech.car.dao.CarDao;
import com.int_tech.car.dao.CarInfoDao;
import com.int_tech.car.dao.CarInfoDataDao;
import com.int_tech.car.entity.Car;
import com.int_tech.car.entity.CarInInfo;
import com.int_tech.car.entity.CarInfoData;
import com.int_tech.mession.dao.MessionDao;
import com.int_tech.mession.entity.Mession;
import com.int_tech.messionLog.dao.MessionLogDao;
import com.int_tech.messionLog.entity.MessionLog;
import com.int_tech.station.dao.StationDao;
import com.int_tech.station.entity.Station;
import com.int_tech.utils.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * Created by liubochao on 2018/8/8.
 */
@Component
public class DeleteMessionLogTimeTask {

    @Autowired
    private MessionLogDao messionLogDao;

    @Autowired
    private CarInfoDataDao carInfoDataDao;

    @Autowired
    private CarDao carDao;

    @Autowired
    private MessionDao messionDao;

    @Autowired
    private StationDao stationDao;

    @Autowired
    private CarInfoDao carInfoDao;

    /**
     * 定时删除messionLog里的数据
     *
     * @throws ParseException
     */
    @Scheduled(cron = "0 0 0/3 * * ?")
    public void deleteMessionLogOnTime() throws ParseException {


        String month = DateTool.Date2String(new Date(),"yyyy-MM-dd");

        String date = month + " 00:00:00";

        Date onDate = DateTool.string2Date(date,"yyyy-MM-dd HH:mm:ss");

        List<MessionLog> messionLogList = messionLogDao.findByCreateTimeBefore(onDate);

        messionLogDao.delete(messionLogList);

    }


    /**
     * 工时车辆找场站
     * @throws ParseException
     */
    @Scheduled(cron = "0/50 * * * * ?")
    public void findStation() throws ParseException {

        List<CarInfoData> carInfoDataList = carInfoDataDao.findByStationId(0);

        List<Car> cars = carDao.findCarByIsDel(0);

        for (CarInfoData carInfoData : carInfoDataList)
        {
            try {

                for (Car car : cars)
                {
                    if(car.getCarNumber().equals(carInfoData.getCarNumber()))
                    {
                        carInfoData.setStationId(car.getStationId());

                        carInfoDataDao.save(carInfoData);
                        break;
                    }
                }
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }


        List<CarInInfo> carInInfoList = carInfoDao.findByStationId(0);

        for (CarInInfo carInInfo : carInInfoList)
        {
            try {

                for (Car car : cars)
                {
                    if(car.getCarNumber().equals(carInInfo.getCarCnumber()))
                    {
                        carInInfo.setStationId(car.getStationId());

                        carInfoDao.save(carInInfo);
                        break;
                    }
                }
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }

    }


    /**
     * 出入刚信息
     * @throws ParseException
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void churugang() throws ParseException {

        List<Mession> messionList = messionDao.findByMessionCountAndIsOverAndCheckStatus(0,1,3);

        List<Station> stations = stationDao.findAll();

        for(Mession mession:messionList)
        {
            mession.setMessionCount(1);
            messionDao.save(mession);


            try {
                CarInInfo carInInfoIn = new CarInInfo();

                carInInfoIn.setCarCnumber(mession.getCarNumber());
                carInInfoIn.setCarNumber(mession.getCarNumber());
                carInInfoIn.setStationName(mession.getEndAddress());
                carInInfoIn.setCreateTime(mession.getEndOpenDoorTime());
                carInInfoIn.setInTime(DateTool.Date2String(mession.getEndOpenDoorTime(),"yyyy-MM-dd HH:mm:ss"));
                carInInfoIn.setStatus(1);

                if(!mession.getEndAddress().contains("场站外"))
                {
                    carInfoDao.save(carInInfoIn);
                }


                CarInInfo carInInfoOut = new CarInInfo();

                carInInfoOut.setCarCnumber(mession.getCarNumber());
                carInInfoOut.setCarCnumber(mession.getCarNumber());
                carInInfoOut.setStationName(mession.getStartAddress());
                carInInfoOut.setCreateTime(mession.getStartCloseDoorTime());
                carInInfoOut.setInTime(DateTool.Date2String(mession.getStartCloseDoorTime(),"yyyy-MM-dd HH:mm:ss"));
                carInInfoOut.setStatus(2);

                if(!mession.getStartAddress().contains("场站外"))
                {
                    carInfoDao.save(carInInfoOut);
                }
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }


    /**
     * 校验场站
     * @throws ParseException
     */
    @Scheduled(cron = "0/59 * * * * ?")
    public void checkStation() throws ParseException {

        List<Mession> messionList = messionDao.findByCheckStatusAndStartAddressLike(3,"场站外");
        List<Station> stationList = stationDao.findAll();

        int m = 0;
        for(Mession mession : messionList)
        {
            m++;

            //经纬度
            String startAddress = mession.getStartAddress();
            String jinweidu = startAddress.substring(startAddress.lastIndexOf("(")+1,startAddress.lastIndexOf(")")-1);

            String slat = jinweidu.split(",")[0];
            String slng = jinweidu.split(",")[1];


//            String slat = "112.919266";
//            String slng = "23.225754";


            Boolean isInStaion = false;

            //所在场站
            Station stationStartIn = new Station();

            for (Station station:stationList)
            {
                try
                {
                    String partitionLocation = "";

                    //场站坐标数组
                    String[] points = station.getArea().split("&");

                    int m1 =0;
                    for (String point:points)
                    {
                        try
                        {
                            if(m1 == 0)
                            {
                                partitionLocation = point.split(",")[1]+"_"+point.split(",")[0];
                            }
                            else
                            {
                                partitionLocation += ","+point.split(",")[1]+"_"+point.split(",")[0];
                            }

                            m1++;
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }

                    String[] parts = partitionLocation.split("_");

                    if(parts.length > 3)
                    {
                        //校验坐标点，如果在场站里，直接跳出，如果全部过滤都不在，存储信息
                        isInStaion = OrderMapTest.isInPolygon(slat,slng,partitionLocation);
                    }
                    else
                    {
                        //校验坐标点，如果在场站里，直接跳出，如果全部过滤都不在，存储信息
//                            System.out.print("要处理的位置信息为"+partitionLocation);
                        isInStaion = OrderMapTest.isInPolygonSingle700(slat,slng,partitionLocation);
                    }

                    if(isInStaion)
                    {
                        stationStartIn = station;
                        break;
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }

            }
            if(isInStaion)
            {
                mession.setStartAddress(stationStartIn.getName());

                messionDao.save(mession);

                stationStartIn.setArea(jinweidu);
                stationStartIn.setUpdateTime(new Date());

                stationDao.save(stationStartIn);
            }
        }

        System.out.print(m);
    }


    @Scheduled(cron = "0/59 * * * * ?")
    public void EndCheckStation() throws ParseException {

        List<Mession> messionList = messionDao.findByCheckStatusAndEndAddressLike(3,"场站外");
        List<Station> stationList = stationDao.findAll();

        int m = 0;
        for(Mession mession : messionList)
        {
            m++;

            //经纬度
            String endAddress = mession.getEndAddress();
            String jinweidu = endAddress.substring(endAddress.lastIndexOf("(")+1,endAddress.lastIndexOf(")")-1);

            String slat = jinweidu.split(",")[0];
            String slng = jinweidu.split(",")[1];


//            String slat = "112.919266";
//            String slng = "23.225754";


            Boolean isInStaion = false;

            //所在场站
            Station stationStartIn = new Station();

            for (Station station:stationList)
            {
                try
                {
                    String partitionLocation = "";

                    //场站坐标数组
                    String[] points = station.getArea().split("&");

                    int m1 =0;
                    for (String point:points)
                    {
                        try
                        {
                            if(m1 == 0)
                            {
                                partitionLocation = point.split(",")[1]+"_"+point.split(",")[0];
                            }
                            else
                            {
                                partitionLocation += ","+point.split(",")[1]+"_"+point.split(",")[0];
                            }

                            m1++;
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }

                    String[] parts = partitionLocation.split("_");

                    if(parts.length > 3)
                    {
                        //校验坐标点，如果在场站里，直接跳出，如果全部过滤都不在，存储信息
                        isInStaion = OrderMapTest.isInPolygon(slat,slng,partitionLocation);
                    }
                    else
                    {
                        //校验坐标点，如果在场站里，直接跳出，如果全部过滤都不在，存储信息
//                            System.out.print("要处理的位置信息为"+partitionLocation);
                        isInStaion = OrderMapTest.isInPolygonSingle700(slat,slng,partitionLocation);
                    }

                    if(isInStaion)
                    {
                        stationStartIn = station;
                        break;
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }

            }
            if(isInStaion)
            {
                mession.setEndAddress(stationStartIn.getName());

                messionDao.save(mession);

                stationStartIn.setArea(jinweidu);
                stationStartIn.setUpdateTime(new Date());

                stationDao.save(stationStartIn);
            }
        }

        System.out.print(m);
    }


    /**
     * 校验出入港车牌号为空
     * @throws ParseException
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void checkCarInCarNumberEmpty() throws ParseException {

        List<CarInInfo> carInInfoList = carInfoDao.findByCarNumberIsNull();

        for(CarInInfo carInInfo : carInInfoList)
        {
            carInInfo.setCarNumber(carInInfo.getCarCnumber());
        }

        carInfoDao.save(carInInfoList);

    }
}
